package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.CommentLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

@DubboService
public class CommentLikeApiImpl implements CommentLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //判断是否已点赞
    @Override
    public boolean haslike(String commentId, Long userId) {
        Criteria criteria = Criteria.where("commentId").is(new ObjectId(commentId))
                .and("userId").is(userId);
        Query query = Query.query(criteria);
        boolean flag = mongoTemplate.exists(query, CommentLike.class);
        return flag;
    }


    //删除点赞
    @Override
    public void delete(String commentId, Long userId) {
        Query query = Query.query(Criteria.where("commentId").is(new ObjectId(commentId))
                .and("userId").is(userId));
        mongoTemplate.remove(query, CommentLike.class);
    }


    //保存点赞
    @Override
    public void save(CommentLike commentLike) {
        mongoTemplate.save(commentLike);
    }

}
